//
//  ZYCStuAttendanceStatisticsViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/19.
//

#import "ZYCStuAttendanceStatisticsViewController.h"
#import "ZYCSelectPastDateView.h"
#import "ZYCStuAttendanceStatistCell.h"
#import "ZYCStuAttendanceStatisticsDetailViewController.h"
#import "ZYCAttendanceStatisticsModel.h"
#import "ZYCTourRecordMenuView.h"
#import "ZYCCommonSelectObjectPickerView.h"
#import "ZYCCommonBaseModel.h"
#import "ZYCNoDataView.h"
static NSInteger pageSize = 10;
@interface ZYCStuAttendanceStatisticsViewController ()<UITableViewDelegate,UITableViewDataSource,ZYCTourRecordMenuViewDelegate,ZYCCommonSelectObjectPickerViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZYCTourRecordMenuView *menuView;
@property (nonatomic, strong) NSArray *gradeArr;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) ZYCSelectPastDateView *dateSelectView;
@property (nonatomic, strong) ZYCCommonSelectObjectPickerView *gradePickerView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, copy) NSString *grade;
@property (nonatomic, copy) NSString *attendanceDate;
@property (nonatomic, strong) ZYCNoDataView *noDataView;
@end

@implementation ZYCStuAttendanceStatisticsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"考勤统计";
    
    [self.view addSubview:self.menuView];
    [self.view addSubview:self.tableView];

    // 默认查询当天  默认展示全部年级
    self.attendanceDate  = [self getCurrentTime];
    self.menuView.leftStr = self.attendanceDate;
    [self loadNewData];
}

// 年级统计列表
- (void)loadNewData{
    self.pageNum = 1;
    NSDictionary *params = @{
        @"pageNum":@(self.pageNum),
        @"pageSize":@(pageSize),
        @"attendanceDate":self.attendanceDate ? self.attendanceDate : @"",// 考勤日期
        @"grade":self.grade ? self.grade : @""//年级
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_GradeAttendanceCensus requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [MrjSVProgressHUD dismiss];
        if (SUCCESS(responseObject[@"code"])) {
            if (SUCCESS(responseObject[@"code"])) {
                [self.dataSource removeAllObjects];
                NSArray *array  =[NSArray yy_modelArrayWithClass:[ZYCAttendanceStatisticsModel class] json:responseObject[@"body"][@"list"]];
                [self.dataSource addObjectsFromArray:array];
                [self.tableView reloadData];
                [self reset];
                [self.tableView.mj_header endRefreshing];
                if (self.dataSource.count == 0) {
                    self.noDataView.hidden = NO;
                    self.tableView.hidden = YES;
                }else{
                    self.noDataView.hidden = YES;
                    self.tableView.hidden = NO;
                }
            }
        }else{
            [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
        }
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}


-(void)loadMoreData{
    self.pageNum ++;
    NSDictionary *params = @{
        @"pageNum":@(self.pageNum),
        @"pageSize":@(pageSize),
        @"attendanceDate":self.attendanceDate ? self.attendanceDate : @"",// 考勤日期
        @"grade":self.grade ? self.grade : @""// 年级
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_GradeAttendanceCensus requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [MrjSVProgressHUD dismiss];
        if (SUCCESS(responseObject[@"code"])) {
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *array  =[NSArray yy_modelArrayWithClass:[ZYCAttendanceStatisticsModel class] json:responseObject[@"body"][@"list"]];
                if (array.count == 0) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.dataSource addObjectsFromArray:array];
                    [self.tableView reloadData];
                    [self.tableView.mj_footer endRefreshing];
                }
            }
        }else{
            [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
        }
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)reset{
    [self.tableView.mj_footer setRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_footer resetNoMoreData];
}

- (void)tourRecordMenuViewClickLeft{
    [self.dateSelectView show];
}
- (void)tourRecordMenuViewClickRight{
    [self.gradePickerView show];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCStuAttendanceStatistCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCStuAttendanceStatistCell class]) forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.section];
    [cell setSectionCircularTableView:tableView withIndexPath:indexPath];
    return cell;

}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ZYCAttendanceStatisticsModel *model = self.dataSource[indexPath.section];    
    ZYCStuAttendanceStatisticsDetailViewController *vc = [ZYCStuAttendanceStatisticsDetailViewController new];
    vc.model = model;
    vc.attendanceDate = self.attendanceDate;
    [self.navigationController pushViewController:vc animated:YES];

}

#pragma mark - ZYCCommonSelectObjectPickerViewDelegate
- (void)selectObjectPickerView:(ZYCCommonSelectObjectPickerView *)pickerView DidClickModel:(ZYCCommonBaseModel *)model{
    if (pickerView == self.gradePickerView) {
        self.grade = model.nameId;
        self.menuView.rightStr = model.name;
        [self loadNewData];
    }
}


- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}

- (ZYCNoDataView *)noDataView {
    if (!_noDataView) {
        _noDataView = [[ZYCNoDataView alloc] initWithFrame:self.tableView.frame];
        _noDataView.hidden = YES;
        [self.view addSubview:_noDataView];
    }
    return _noDataView;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.menuView.frame), kScreenWidth, kScreenHeight - CGRectGetMaxY(self.menuView.frame)-safeAreaBottomHeight) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCStuAttendanceStatistCell class] forCellReuseIdentifier:NSStringFromClass([ZYCStuAttendanceStatistCell class])];

    }
    return _tableView;
}


- (ZYCCommonSelectObjectPickerView *)gradePickerView {
    if (!_gradePickerView) {
        _gradePickerView = [[ZYCCommonSelectObjectPickerView alloc] init];
        _gradePickerView.dataSource = self.gradeArr;
        _gradePickerView.tipString = @"请选择年级";
        _gradePickerView.delegate = self;
        [self.view addSubview:_gradePickerView];
    }
    return _gradePickerView;
}


- (ZYCSelectPastDateView *)dateSelectView {
    ZYCWeakSelf
    if (!_dateSelectView) {
        _dateSelectView = [[ZYCSelectPastDateView alloc] init];
        _dateSelectView.tipString = @"请选择时间";
        _dateSelectView.currentDate = [NSDate date];
        _dateSelectView.confirmAction = ^(NSString * _Nonnull dateStr) {
            weakSelf.attendanceDate = dateStr;
            weakSelf.menuView.leftStr = dateStr;
            [weakSelf loadNewData];
        };
        
        [self.view addSubview:_dateSelectView];
    }
    return _dateSelectView;
}

- (ZYCTourRecordMenuView *)menuView {
    if (!_menuView) {
        _menuView = [[ZYCTourRecordMenuView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, 46.0f)];
        _menuView.leftStr = @"日期";
        _menuView.rightStr = @"全部年级";
        _menuView.delegate = self;
    }
    return _menuView;
}


- (NSString *)getCurrentTime {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

- (NSArray *)gradeArr {
    if (!_gradeArr) {
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *path = [documentPath stringByAppendingPathComponent:@"grade.data"];;
        NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        _gradeArr = array;
    }
    return _gradeArr;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

